package com.tgh.help;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;

import com.tgh.help.adapter.UploadSingleGridAdapter;
import com.tgh.help.adapter.ViewPageAdapter;

public class UploadFileFragment extends Fragment {
	private String TAG = "UploadFileFragment";
	private Context mContext;
	/** 每页显示的数量，Adapter保持一致. */
	private static final float PAGE_SIZE = 18.0f;
	/** 每一屏为GridView. */
	private GridView gridView;
	/** ViewPager控件，横屏滑动*/
	private ViewPager filePager;
	/** ViewPager的视图列表*/
	private List<View> mListViews;
	/** ViewPager的适配器. */
	private ViewPageAdapter viewPageAdapter;
	/** 上传文件显示. */
	private LinearLayout uploadLayout;
	/** 没有外设存储显示. */
	private LinearLayout noSDCardLayout;
	private Button btnSelectAll;
	private Button btnUpload;
	private Button btnCancel;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_upload_grid, null);
		filePager = (ViewPager) view.findViewById(R.id.uploadViewPager);
		uploadLayout = (LinearLayout) view.findViewById(R.id.uploadLayout);
		noSDCardLayout = (LinearLayout)view.findViewById(R.id.noSDCardLayout);
		btnSelectAll = (Button) view.findViewById(R.id.btnSelectAll);
		btnUpload = (Button) view.findViewById(R.id.btnUpload);
		btnCancel = (Button) view.findViewById(R.id.btnCancel);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mContext = getActivity();
		mListViews = new ArrayList<View>();
		if(checkExternalStorageState()){
			noSDCardLayout.setVisibility(View.GONE);
			uploadLayout.setVisibility(View.VISIBLE);
			ArrayList<String> list = new ArrayList<String>();
			for(int i = 0; i<100; i++){
				list.add("文件1");
			}
			setGrid(mContext, list);
			viewPageAdapter = new ViewPageAdapter(mListViews);
			filePager.setAdapter(viewPageAdapter);
			filePager.setCurrentItem(0);
		}else{
			uploadLayout.setVisibility(View.GONE);
			noSDCardLayout.setVisibility(View.VISIBLE);
		}
		
		btnUpload.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				UploadActivity uploadActivity=(UploadActivity) getActivity();
				uploadActivity.showUploading();
			}
		});
		
	}
	
	public Button getUploadButton(){
		return btnUpload;
	}
	public Button getSelectAllButton(){
		return btnSelectAll;
	}
	public Button getCancelButton(){
		return btnCancel;
	}
	
	 private void setGrid(Context context, ArrayList<String> list) {
			int PageCount = (int) Math.ceil(list.size() / PAGE_SIZE);
			for (int i = 0; i < PageCount; i++) {
				gridView = (GridView) LayoutInflater.from(context).inflate(
						R.layout.item_upload_grid, null);
				gridView.setAdapter(new UploadSingleGridAdapter(context, list, i));
				
				// 去掉点击时的黄色背景
				gridView.setSelector(R.color.transparent);
				mListViews.add(gridView);
			}
		}
	 
	 private boolean checkExternalStorageState(){
		 Log.d(TAG, "--------------------"+Environment.getExternalStorageState());
		 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			 return true;
		 }
		 return false;
	 }
	    
}
